package org.teachertools.web.forms;

import org.teachertools.common.ValidationUtils;
import org.teachertools.data.Categorie;
import org.teachertools.data.Competence;
import org.teachertools.data.Matiere;

public class CompetenceFormMapping {

    public static Competence mappingFormToDTO(CompetenceForm competenceForm) {
        return mappingFormToDTO(competenceForm, new Competence());
    }

    public static Competence mappingFormToDTO(CompetenceForm competenceForm, Competence competence) {
        if (ValidationUtils.isNotEmpty(competenceForm.getId())) {
            Long idTypedValue = competenceForm.getId();
            competence.setId(idTypedValue);
        }
        if (ValidationUtils.isNotEmpty(competenceForm.getIdMatiere())) {
            Matiere matiereTypedValue = new Matiere();
            Long id = competenceForm.getIdMatiere();
            if (id != 0) {
                matiereTypedValue.setId(id);
            }
            competence.setMatiere(matiereTypedValue);
        }
        if (ValidationUtils.isNotEmpty(competenceForm.getIdCategorie())) {
            Categorie cat = new Categorie();
            Long id = competenceForm.getIdCategorie();
            if (id != 0) {
                cat.setId(id);
            }
            if (competence.getMatiere() == null) {
                competence.setMatiere(new Matiere());
            }
            competence.getMatiere().setCategorie(cat);
        }
        if (ValidationUtils.isNotEmpty(competenceForm.getNom())) {
            java.lang.String nomTypedValue = competenceForm.getNom();
            competence.setNom(nomTypedValue);
        }
        return competence;
    }

    public static void mappingDTOToForm(Competence competence, CompetenceForm competenceForm) {
        competenceForm.reset();

        if (ValidationUtils.isNotEmpty(competence.getId())) {
            competenceForm.setId(competence.getId());
        }

        if (ValidationUtils.isNotEmpty(competence.getNom())) {
            competenceForm.setNom(competence.getNom());
        }

        if (ValidationUtils.isNotEmpty(competence.getMatiere())) {
            competenceForm.setIdMatiere(competence.getMatiere().getId());
            if (ValidationUtils.isNotEmpty(competence.getCategorie())) {
                competenceForm.setIdCategorie(competence.getCategorie().getId());
            }
        }
    }
}
